package com.hyk.utils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.hyk.entity.AlipayBean;

import java.util.Map;

/**
 * @Author: chen
 * @Date: 2021/3/23 5:50 PM
 * @Version 1.0
 */
/* 支付宝 */
public class AlipayUtil {

    public static final String app_id = "2016091100484175";
    public static final String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCNaYRM+H0TmwLxM+B1AOM1uW15L4+4ozxtXGR6ozJvi8a/TqLM2sLvhBeJdMUnjMxRTHEuEuvvkYIpgEl2yZ2FertKR/vb3Leyiw92ff7qKe9oa7jlMUMZcthSPqB7tkq5ejVSgr4Yd6mmGQF04q5Eh+BRxWiiyAWWdthI9ZAHne6CnLyPDBOL9gqa6vnif7Fmcku+71E2lFgm6PPzQUUgUDhfW2WumqrpIkkJGJQmUFjtmVkSnbQbnNL1xgEJIbmBDUB9DYfpfaIl7EGyVWnFibu6nbNJ9kwjKIuXIsLXjDp+j0hBd1QLh0SDU1zgQ+xadwI6Shk2KQd2jdw8NjMFAgMBAAECggEAagKSJcmaqlnoiL2lJNF3g0zw3opjKb1ljW5X/P/ezXRxl/TxYrUUsc3Kf0dEjXOlr8SojCuvJ7djZo0/hYd7SRdK1FLGYnpCU9yfyOqHcS/iw9sNjvOxI6DOvkkosdVki0ssMiDFNT5vtY7FYsSUc9eQuHxPBskAN138oiVExg/n0WxHvzmAxieDnNRktZ+0zKoWaL0YDeSswia7I5o/QA/JMOiPK318y/pe/3iWdhvEuTATKit3iJBSeLsA7RIoGV1pw7QpntBeSh1UShvrTRl0c56BN+UrSR/QUYrKdauVime6jD9Fo1oZwYHTroAtT1dxn1lq2K5qqbmn9AMjwQKBgQDEshFYlnXusqG9PQbpFE8zK6cmjdIlxKDCPUG+HS0oRkygWQ/CX6yS98zp2H5a0uQMLpMFv2trx5e0Y2spu0SgN7VlALDXn4HhX5xstiZsb9ZJMjOSuJsIPZWYrHNAeS+0ea4Ndr8m0bQwCKHu1NT18jOLj+LNLqjjzxNgr/9L6QKBgQC4DGdzoF+z8i7ECwflhoplTMawlVFQXN2H2ZtiwsgNqodwIMxXSNHZqD4bOQZoepB6cvyMZr5Yxd0F7iC5Gk2/TQDtWkDgq47y1hIlYM+TMC4j200EHZAltw7R2mwqply3F9m1/jOmMYUkvRRCc1W5GeF6JxQyPxTS5gk5qnDovQKBgGNYgPRaglQ+evyGr0/YByyUNsd9SA/1YTDaGbN5Lw6xexBeC1ykUBim+iN+Skt7St1wRKfZh9sXI2Nj94NLZ5z4pjDOiYNOuB3p2ar60SthzTyJE41emkcuO7myGEAPNW4VKzj3qhJkRnsgURG9A5b5btlloir6DymItIPYQLzJAoGAAPgHv2MTveXDe0K3muy3Y8sgrKNMl0i13dY2bDGsTe4c4mk5yifW+vdYxFnrf7dNdWePjsmnrN31yOc1AuRxjlVAcP9togElMoAP/mRhE1xIkeXApQnmzVwGVvJ4aU0Q5eHZQo0BBpnyInxgU+05gUzyk+sKvyz31hhh6gzMpV0CgYAEhnTjXzuJNvUHYRKEZfyO4STG48ptjaj9O3hmDkEIDV63byhdwOJAHe4GaZMmmLr7JCy2TgIR2+ojFG6CHxzTaIeGOzEXHJ4FptLKIhh74pydwHnP0/+Y0KW/QbraFyfbevkEITWLZLwKqIvJu5RPCc4fLqRcNys0seQPzEsXnA==";
    public static final String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA827KiAW7uTnI9yWXdV0A2SharMga+U50Epfh5ZwnT8NIQzQXM47wjcon0TzCKkVSHrvxWuLvSLoBxmd5wmQhBnwf0Cvs6qpSroa0v9N6slyAQxeLryjK1tnDtxdboUosElicSzLvEeriSebaCH0Tfba8hl1+6CBiP4+2UaPW6Dub3zCO4XJ7UElpHNhx9b6yaZvpR/nq8n+NXzZRfWa4P5de4b/6U04u38bWZlmGV2LEn9DEdE+uoAOO6oOGzFsicQ7OgBNuUoyBNNpygE7CqTDh473qDw93VzSVAoS/pi2+6+vWdPUrjxjjgUKl1vc0kvmzmdQzypwQG1NKLnzx0wIDAQAB";


    public static final String notify_url = "http://1.15.177.112:8080/order/alipay_notify";
    public static final String return_url = "http://1.15.177.112:8080/order/alipay_callback";

    //加密方法
    public static final String sign_type = "RSA2";
    //字符集编码
    public static final String charset = "utf-8";
    //支付宝网关
    public static final String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public static String connect(AlipayBean alipayBean) throws AlipayApiException {
        //1、获得初始化的AlipayClient
        AlipayClient alipayClient = getAlipayClient();
        //2、设置请求参数
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        //页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(return_url);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notify_url);
        //封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        //3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //返回付款信息
        return result;
    }

    //查询订单
    public static AlipayTradeQueryResponse search(Map<String,String> map) throws AlipayApiException {
        //1、获得初始化的AlipayClient
        AlipayClient alipayClient = getAlipayClient();
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent(JSON.toJSONString(map));
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        //返回付款信息
        return response;
    }

    private static AlipayClient getAlipayClient() {
        return new DefaultAlipayClient(
                gatewayUrl,//支付宝网关
                app_id,//appid
                merchant_private_key,//商户私钥
                "json",
                charset,//字符编码格式
                alipay_public_key,//支付宝公钥
                sign_type//签名方式
        );
    }
}
